package xyf.p07.binsearch;

/**
 * ClassName: P0069_MySqrt
 * Description:
 * Author: xyf
 * Date: 9/7/22 6:28 PM
 * Version: 1.0
 **/
public class P0069_MySqrt
{
    public int mySqrt(int x)
    {
        int start = 0;
        int end = x;
        int result = -1;
        while (start <= end)
        {
            int middle = start + (end - start) / 2;
            long num = (long) middle * middle;
            if (num > x)
            {
                end = middle - 1;
            }
            else if (num < x)
            {
                result = middle;
                start = middle + 1;
            }
            else
            {
                return middle;
            }
        }
        return result;
    }
}
